package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.IsChangeIntent;
import com.lanou.lenanapp.util.Utils;
import com.lanou.lenanapp.view.CustomDialog;

import java.util.List;

/**
 * Created by dllo on 15/10/27.
 * 设置界面
 */
public class SetActivity extends Activity implements View.OnClickListener {

    private RelativeLayout question, cleanBtn, edition, enterChange, mainActivity, contributionActivity;
    private ImageView back_set, img_loading;
    private TextView setTv;
    private Animation animation ;
    private DatabaseHelper helper;
    private Dialog dialog;
    private List<IsChangeIntent> list;
    private SharedPreferences sharedPreferences;
    private Handler handler;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 4000);
            dialog.dismiss();//关闭动画弹出框
            showDialog();//弹出提示框
            handler.removeCallbacks(runnable);//关闭线程
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_set);
        helper = DatabaseHelper.getHelper();//初始化helper帮助类
        handler = new Handler();
        init();
    }

    private void init() {
        question = (RelativeLayout) findViewById(R.id.question);
        question.setOnClickListener(this);//常见问题
        cleanBtn = (RelativeLayout) findViewById(R.id.cleanBtn);
        cleanBtn.setOnClickListener(this);//清空缓存
        back_set = (ImageView) findViewById(R.id.back_set);
        back_set.setOnClickListener(this);//返回主页面
        edition = (RelativeLayout) findViewById(R.id.edition);
        edition.setOnClickListener(this);//版本号
        img_loading = (ImageView) findViewById(R.id.img_loading);
        enterChange = (RelativeLayout) findViewById(R.id.enterChange);
        enterChange.setOnClickListener(this);//启动时打开
        setTv = (TextView) findViewById(R.id.setTv);

        //取判断选择页面的数据库取出ischange属性
        //设置进入设置页面时显示的状态
        list = helper.queryChange();
        IsChangeIntent isChangeIntent = list.get(0);
        int t = isChangeIntent.getIsChange();
        if (t == 1){
            setTv.setText("官方发布区");
        }else if (t == 0){
            setTv.setText("网友投稿区");
        }

        //加载动画
        animation = AnimationUtils.loadAnimation(this, R.anim.loading_dialog);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.question:
                Toast.makeText(this, "有问题么? ", Toast.LENGTH_SHORT).show();
                break;
            case R.id.cleanBtn://清空缓存
                ChangeClean();
                break;
            case R.id.back_set:
                this.finish();//返回主页面
                break;
            case R.id.edition://版本号
                dialog = Utils.createloadingDialog(this, "加载中...");
                dialog.show();
                handler.postDelayed(runnable, 3000);
                break;
            case R.id.enterChange:
                isChangeEnter(enterChange);
                break;
        }
    }

    /**
     * 选择启动时打开时  弹出popupwindow
     * **/
    private void isChangeEnter(View achor) {
        final PopupWindow window = new PopupWindow(this);
        View v = getLayoutInflater().inflate(R.layout.activity_set_popupwindow, null);
        mainActivity = (RelativeLayout) v.findViewById(R.id.mainActivity);
        contributionActivity = (RelativeLayout) v.findViewById(R.id.contributionActivity);

        window.setContentView(v);
        window.setWidth(300);
        window.setHeight(300);
        window.setOutsideTouchable(true);
        window.showAsDropDown(enterChange);
        //点击官方发布区设置
        mainActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = helper.queryChange();
                IsChangeIntent isChangeIntent = list.get(0);
                int t = isChangeIntent.getIsChange();
                if (t == 1){

                }else if (t == 0){
                    t++;
                    isChangeIntent.setIsChange(t);
                    helper.saveChange(isChangeIntent);
                }
                setTv.setText("官方发布区");
                Toast.makeText(SetActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                window.dismiss();
            }
        });
        //点击网友投稿区设置
        contributionActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = helper.queryChange();
                IsChangeIntent isChangeIntent = list.get(0);
                int t = isChangeIntent.getIsChange();
                if (t == 1){
                    t--;
                    isChangeIntent.setIsChange(t);
                    helper.saveChange(isChangeIntent);
                }else if (t == 0){

                }
                setTv.setText("网友投稿区");
                Toast.makeText(SetActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                window.dismiss();
            }
        });
    }


    /***
     * 清空缓存弹出提示框
     * **/
    private void ChangeClean() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("清空缓存可以节省空间,但保留缓存可以使浏览过得图片加载更快,是否确认清空?");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("确认清空", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //清除数据库内容
                helper.deleteAllJoke();//清空主页数据库
                helper.deletaAllImageList();
                helper.deleteAllCollect();//清空收藏数据库
                //helper.deleteAllLoginOne();
                Toast.makeText(SetActivity.this, "缓存清除成功!", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();//显示弹出框
    }

    //版本号第一次弹出的
    private void showDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("当前版本号为1.1.9\n 要更新么?");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showTwoDialog();
                handler.removeCallbacks(runnable);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
    //版本号第二次弹出的
    private void showTwoDialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("您当前已经是最新版本!!!!");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("好吧", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(SetActivity.this, "闲的是不是??", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

}
